<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreMenuRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'parent_id' => 'nullable|exists:menus,id',
            'title' => 'required|string|max:255',
            'name' => 'nullable|string|max:255',
            'path' => 'nullable|string|max:255',
            'component' => 'nullable|string|max:255',
            'icon' => 'nullable|string|max:255',
            'type' => 'required|integer|in:' . implode(',', [
                \App\Models\Menu::TYPE_DIRECTORY,
                \App\Models\Menu::TYPE_MENU,
                \App\Models\Menu::TYPE_BUTTON,
            ]),
            'permission' => 'nullable|string|max:255',
            'status' => 'required|integer|in:' . implode(',', [
                \App\Models\Menu::STATUS_DISABLED,
                \App\Models\Menu::STATUS_ENABLED,
            ]),
            'sort' => 'nullable|integer',
            'hidden' => 'nullable|integer|in:0,1',
            'keep_alive' => 'nullable|integer|in:0,1',
            'remark' => 'nullable|string',
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages(): array
    {
        return [
            'title.required' => '菜单标题不能为空',
            'type.required' => '菜单类型不能为空',
            'type.in' => '菜单类型值不正确',
            'status.required' => '状态不能为空',
            'status.in' => '状态值不正确',
            'parent_id.exists' => '父菜单不存在',
        ];
    }
}

